package com.kayak.emailWorks.quartz.demo;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.springframework.beans.factory.annotation.Autowired;

import com.kayak.dbutil.dao.ComnDao;
import com.kayak.dbutil.redis.JedisEx;
import com.kayak.emailWorks.util.FetchEmail;
import com.kayak.exception.ErrorException;
import com.kayak.kkweb.sys.SysBeans;

public class GetDailyReport {
	
	@Autowired
	protected ComnDao comnDao;
	@Autowired
	protected JedisEx jedisEx;

	public static void doIt() throws IOException {
    	String username = "lux@kayak.com.cn";
        String password = "Lxaktxl950626";
        List<Map<String,Object>>
        newMsgList=null;
        try {
        	newMsgList=new FetchEmail().getNewEmails(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
        for(Map<String, Object> temp:newMsgList){
        	try {
        		new GetDailyReport().dealWithReturnMsg(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
    }

	public Map<String,Object> dealWithReturnMsg(Map<String, Object> temp) throws ErrorException{
		ComnDao comnDao = SysBeans.getComnDao();
		Scanner sr=new Scanner((Readable) temp);
		Map<String,Object> map =null;
		Map<String,Object> map1 =null;
		while(sr.hasNext()){
			String line =sr.nextLine();
			if(line.contains("日期：")){
				map = new  HashMap<String,Object>();
				map.put("report_date", line.substring(line.lastIndexOf("：")+1).trim());
			}
			if(line.contains("姓名：")){
				map.put("name", line.substring(line.lastIndexOf("：")+1).trim());
				comnDao.update("Ed1", map);
			}
			if(line.contains("每日周报提醒邮件回复内容")){
				map1 = new  HashMap<String,Object>();
				map1.putAll(map);
			}
			if(line.contains("项目名称：")){
				map1.put("project_name", line.substring(line.lastIndexOf("：")+1).trim());
				/*comnDao.update("Ed2", map1);*/
			}
			if(line.contains("工时：")){
				map1.put("work_hour", line.substring(line.lastIndexOf("：")+1).trim());
			}
			if(line.contains("所在城市：")){
				map1.put("work_city", line.substring(line.lastIndexOf("：")+1).trim());
			}
			if(map1!=null&&line.contains("所在城市：")){
				comnDao.update("Ei1", map1);
			}
		}
		
		return map;
	}

}
